import {
    StationDetail
} from './station-detail.model'
const stationDetail = new StationDetail()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        station: {
            company_name: '检车站',
            logo_url: '/images/noimg.png',
            company_addr: '南岗区铁路街1号',
            longitude: '126.63752',
            latitude: '45.766483',
            telephone: '12345678',
            start_time: '07:30',
            end_time: '17:00'
        },
        apply_url: '/pages/apply/apply',
        comment: [],
        type: 'vehicle-type',
        page: 1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        that._loadData(options)
    },

    /**
     * 获取所有数据
     * @param {*} options
     */
    _loadData(options) {
        let that = this
        that.setData({
            id: options.id
        })
        // that.getOrderNumber()
        that.getDetailData()
        that.getCommentData()
    },

    /**
     * 获取用户订单数
     */
    // getOrderNumber(){
    //     let that = this
    //     stationDetail.getOrderNumber().then((res) => {
    //         console.log(res.data);
    //     })
    // },

    /**
     * 获取检车站详情
     */
    getDetailData() {
        wx.showLoading({
            title: '正在加载数据',
        })
        let that = this,
            id = that.data.id,
            apply_url = that.data.apply_url
        stationDetail.getDetailData(id).then((res) => {
            if (res.data.subscribe_is_open) {
                apply_url = '/pages/subscribe/subscribe'
            }
            that.setData({
                station: res.data,
                apply_url: apply_url
            })
        }).then(() => {
            wx.hideLoading()
        })
    },

    /**
     * 获取评论列表
     */
    getCommentData() {
        let that = this,
            param = {
                id: that.data.id
            }
        stationDetail.getCommentData(param).then((res) => {
            that.setData({
                comment: res.data
            })
        })
    },

    /**
     * 跳转到申请页
     * @param {*} options
     */
    onJumpApply(options) {
        let that = this,
            station = that.data.station,
            apply_url = that.data.apply_url,
            vehicle_name = stationDetail.getDataSet(options, 'vehicle_name'),
            goods_id = stationDetail.getDataSet(options, 'goods_id'),
            goods_name = stationDetail.getDataSet(options, 'goods_name'),
            goods_price = stationDetail.getDataSet(options, 'goods_price'),
            vehicle_id = stationDetail.getDataSet(options, 'vehicle_id'),
            url = `${apply_url}?id=${station.id}&vehicle_name=${vehicle_name}&goods_id=${goods_id}&goods_name=${goods_name}&goods_price=${goods_price}&vehicle_id=${vehicle_id}&station_name=${station.company_name}&station_address=${station.company_addr}&station_number=${station.company_number}`
        wx.navigateTo({
            url: url,
        })
    },

    /**
     * 点击地图事件
     */
    onTapMap() {
        let that = this,
            data = that.data.station
        wx.openLocation({
            latitude: parseFloat(data.latitude),
            longitude: parseFloat(data.longitude),
            scale: 17,
            name: data.company_name,
            address: data.company_addr
        })
    },

    /**
     * 点击拨打电话
     */
    onTapPhone() {
        let that = this
        wx.makePhoneCall({
            phoneNumber: that.data.station.telephone
        })
    },

    /**
     * 切换类型
     * @param {*} options
     */
    onSwitchType(options) {
        let that = this,
            type = stationDetail.getDataSet(options, 'type')
        that.setData({
            type: type
        })
    },

    /**
     * 新页面中全屏预览图片
     * @param {*} options
     */
    onPreviewImage(options) {
        let that = this,
            comment = that.data.comment,
            index = stationDetail.getDataSet(options, 'index')
        wx.previewImage({
            urls: comment[index]['images']
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        wx.showLoading({
            title: '正在加载数据',
        })
        let that = this,
            param = {
                id: that.data.id,
                page: that.data.page + 1
            }
        that.setData({
            page: that.data.page + 1,
        })
        stationDetail.getCommentData(param).then((res) => {
            let result = that.data.comment
            result = result.concat(res.data)
            that.setData({
                comment: result
            })
        }).then(() => {
            wx.hideLoading()
        })
    }
})